<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索列表</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <link rel="stylesheet" href="css/list.css">
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpg" alt="">
        <span class="title">博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer" style="width: 26%"></span>
        <div class="search">
            <input class="search-input" id="search-input" type="text" placeholder="输入文章标题">
            <button class="search-button" onclick="performSearch()">搜索</button>
        </div>

        <a href="blog_list.html">博客广场</a>
        <!-- 未登录显示元素 -->
        <span id="noneLoginElement">
            <a href="login.html">登录</a>
            <a href="reg.html">注册</a>
        </span>

        <!-- 已登录显示元素 -->
        <span id="userLoginElement" style="display: none;">
            <a href="myblog_list.html">个人博客</a>
            <a href="personal_center.html">个人中心</a>
            <a href="my_relation_following.html">我的关注/粉丝</a>
            <a href="draft_list.html">我的草稿</a>
            <a href="blog_add.html">写博客</a>
            <a href="javascript:logout()">注销</a>
        </span>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div id="artlist" class="container-right" style="margin-left: 10%;">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <!-- <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="desc">
                    从今天起, 我要认真敲代码. Lorem ipsum, dolor sit amet consectetur adipisicing elit. Cum distinctio ullam eum ut
                    veroab laborum numquam tenetur est in dolorum a sint, assumenda adipisci similique quaerat vel.
                    Facere,
                    et.
                </div>
                <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;
                <a href="blog_edit.html?id=1" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;
                <a href="javascript:del(1)" class="detail">删除 &gt;&gt;</a>
            </div> -->
        </div>
    </div>


    <script src="js/jquery.min.js"></script>
    <script src="./js/logout.js"></script>
    <script src="./js/islogin.js"></script>
    <script src="./js/urltools.js"></script>

    <script>

        function doSearch() {
            var searchKey = decodeURIComponent(getParamByKey("query"));
            if (searchKey == '') {
                alert("参数错误！");
                return;
            }
            console.log("搜索关键字：" + searchKey);

            jQuery.ajax({
                url: "art/search",
                type: "POST",
                data: {
                    "key": searchKey
                },
                success: function (res) {
                    if (res.code == 200 && res.data.length > 0) {
                        // 获取到搜索结果
                        var arthtml = "";
                        for (var i = 0; i < res.data.length; i++) {
                            var art = res.data[i];
                            arthtml += '<div class="blog">';
                            arthtml += '<div class="title">' + art.title + '</div>';
                            arthtml += '<div class="date">' + art.createtime + '</div>';
                            arthtml += '<div class="desc">';
                            arthtml += art.content;
                            arthtml += '</div>';
                            arthtml += '<a href="blog_content.html?id=' + art.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                            arthtml += '</div>';
                        }
                        jQuery("#artlist").html(arthtml);
                    } else if (res.code == 200 && res.data.length == 0) {
                        // 无搜索结果
                        jQuery("#artlist").html("<h2 style = 'margin-left:100px;margin-top:100px'>无搜索结果</h2>")
                    } else {
                        alert("搜索失败！" + res.msg);
                    }
                    jQuery("#search-input").val(searchKey);
                }
            });
        }

        doSearch();

        function performSearch() {
            var searchKey = jQuery("#search-input");
            if (searchKey.val().trim() == '') {
                alert("请先输入搜索关键字！");
                return;
            }


            var searchUrl = 'search_list.html?query=' + encodeURIComponent(searchKey.val());
            console.log("url：" + searchUrl);

            // 重定向到当前页面
            location.href = searchUrl;
        }
    </script>

</body>

</html>